/**********************************************************
 * ShellUtil.java
 * Created by ankur
 * Last modified Date: Aug 5, 2008 11:41:03 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.util;

import org.apache.log4j.Logger;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * The Class ShellUtil.
 */
public class ShellUtil {
	
	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ShellUtil.class);
	
	/**
	 * Run shell script.
	 * 
	 * @param script the script
	 * @param process the process
	 * 
	 * @return the process
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws InterruptedException the interrupted exception
	 */
	public static synchronized Process runShellScript(String script ,Process process) throws IOException, InterruptedException{
		if (logger.isDebugEnabled()) {
			logger.debug("runShellScript(String, Process) - start"); //$NON-NLS-1$
		}

		if(process==null)process = Runtime.getRuntime().exec(AdminConstants.BASH_START.intern());
		BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
		outCommand.write(script);
		outCommand.flush();
		process.waitFor();

		if (logger.isDebugEnabled()) {
			logger.debug("runShellScript(String, Process) - end"); //$NON-NLS-1$
		}
		return process;
	}
}